<?php
/**
 * HttpRequest.php
 * @author qqinxl
 * Tue Oct 30 12:49:57 GMT 2007
 * 
 * @category  iQin
 * @package  
 * @copyright  Copyright (c) qqinxl
 */
/**
 * 
 * @category  
 * @package   
 * @copyright  Copyright (c) qqinxl
 */
class Util_HttpRequest {
	/**
	 * current url
	 * @return string
	*/
	static public function getCurrentUrl(){
		$_phpself = pathinfo($_SERVER['REQUEST_URI']);
		$_phpself = (isset($_phpself['extension']) and $_phpself['extension']=='php')?$_phpself['dirname']:$_SERVER['REQUEST_URI'];
		if(isset($_SERVER['REDIRECT_HTTPS']) and $_SERVER['REDIRECT_HTTPS'] == 'on'){
			$url = "https://" . $_SERVER["SERVER_NAME"] . $_phpself;
		}elseif(isset($_SERVER['HTTPS'] ) and $_SERVER['HTTPS'] == 'on'){
			$url = "https://" . $_SERVER["SERVER_NAME"] . $_phpself;
		}elseif (isset($_SERVER['SERVER_PROTOCOL']) and strcasecmp(substr($_SERVER['SERVER_PROTOCOL'], 0, 5), "https") == 0){
			$url = "https://" . $_SERVER["SERVER_NAME"] . $_phpself;
		}else{
			$url = "http://" . $_SERVER["SERVER_NAME"] . $_phpself;
		}
		if(substr($url, -1) == '/'){
			$url = substr($url, 0, -1);
		}
		return $url;
	}
	/**
	 * current url
	 * @return array
	*/
	static public function getCurrentUrlInfo(){
		$_phpself = pathinfo($_SERVER['REQUEST_URI']);
		$_phpself = (isset($_phpself['extension']) and $_phpself['extension']=='php')?$_phpself['dirname']:$_SERVER['REQUEST_URI'];
		$url['url'] = $_phpself;
		$url['server'] = $_SERVER["SERVER_NAME"];
		if(isset($_SERVER['REDIRECT_HTTPS']) and $_SERVER['REDIRECT_HTTPS'] == 'on'){
			$url['type'] = "https";
		}elseif(isset($_SERVER['HTTPS'] ) and $_SERVER['HTTPS'] == 'on'){
			$url['type'] = "https" ;
		}elseif (isset($_SERVER['SERVER_PROTOCOL']) and strcasecmp(substr($_SERVER['SERVER_PROTOCOL'], 0, 5), "https") == 0){
			$url['type'] = "https";
		}else{
			$url['type'] = "http";
		}
		return $url;
	}
	/**
	 * previous url
	 * @return string
	*/
	static public function getPreviousUrl(){
		return isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:null;
	}
	/**
	 * ip
	 * @return string
	*/
	static public function getIp(){
		if(getenv("HTTP_CLIENT_IP")&& strcasecmp(getenv("HTTP_CLIENT_IP"),"unknown")){
			$ip = getenv("HTTP_CLIENT_IP");
		}else if(getenv("HTTP_X_FORWARDED_FOR")&& strcasecmp(getenv("HTTP_X_FORWARDED_FOR"),"unknown")){
			$ip = getenv("HTTP_X_FORWARDED_FOR");
		}else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")){
			$ip = getenv("REMOTE_ADDR");
		}else if(isset($_SERVER['REMOTE_ADDR'])&&$_SERVER['REMOTE_ADDR']&&strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")){
			$ip = $_SERVER['REMOTE_ADDR'];
		}else {
			$ip = "unknown";
		}
		return($ip);
	}	
	/**
	 * 根据$_SERVER['HTTP_USER_AGENT'] 判断浏览器是否是手机
	 * @return bool
	*/
	static function isMobile(){
		if(strpos(strtoupper($_SERVER['HTTP_USER_AGENT']),strtoupper("MSIE"))) {
			return false;
		} elseif(strpos(strtoupper($_SERVER['HTTP_USER_AGENT']), strtoupper("NetCaptor"))) {
			return false;
		} elseif(strpos(strtoupper($_SERVER['HTTP_USER_AGENT']),strtoupper("Netscape"))) {
			return false;
		} elseif(strpos(strtoupper($_SERVER['HTTP_USER_AGENT']), strtoupper("Lynx"))) {
			return false;
		} elseif(strpos(strtoupper($_SERVER['HTTP_USER_AGENT']), strtoupper("Safari"))) {
			return false;
		} elseif(strpos(strtoupper($_SERVER['HTTP_USER_AGENT']), strtoupper("Opera"))) {
			return false;
		} elseif(strpos(strtoupper($_SERVER['HTTP_USER_AGENT']),strtoupper("Konqueror"))) {
			return false;
		} elseif(strpos(strtoupper($_SERVER['HTTP_USER_AGENT']),strtoupper( "Mozilla"))) {
			return false;
		} elseif(strpos(strtoupper($_SERVER['HTTP_USER_AGENT']),strtoupper( "Firefox"))) {
			return false;
		}else {
			return true;
		}
	}
	/**
	 * 根据$_SERVER['HTTP_USER_AGENT'] 获得浏览器信息
	 * @return array
	*/
	static function getBrowserInfo()
	{
		$browser = array ( //reversed array
		"Opera",
		"Msie", // parent
		"Netscape",
		"Firefox",
		"Safari",
		"Konqueror",
		"Mozilla", // parent
		"Lynx"
		);

		$info['browser'] = "MOBILE";
		$info['type'] = null;
		foreach ($browser as $parent)
		{
			if ( ($s = strpos(strtoupper($_SERVER['HTTP_USER_AGENT']), strtoupper($parent))) !== FALSE )
			{
				$f = $s + strlen($parent);
				$version = substr($_SERVER['HTTP_USER_AGENT'], $f, 5);
				$version = preg_replace('/[^0-9,.]/','',$version);

				$info['browser'] = $parent;
				$info['version'] = $version;
				$info['type'] = 'pc';
				break; // first match wins
			}
		}
		/*ドコモの例
		タイプ1（MOVA）
		DoCoMo/1.0/N503i/c10
		タイプ2（FOMA）
		DoCoMo/2.0 N901iS(c100;TB;W24H12)
		/c10や(c100)が付かず、機種名で終わる場合もある。
		auの例
		旧タイプ
		UP.Browser/3.04-SN12 UP.Link/3.4.4
		新タイプ
		KDDI-HI21 UP.Browser/6.0.2.254 (GUI) MMP/1.1
		auは機種名ではなく、デバイスタイプ（SN12など）で表記されている。
		デバイスタイプと製品名の対応表は、キャリアのサイトにある。

		ソフトバンクの例
		SoftBank 4-2 Series（Jフォン時代のもの）
		J-PHONE/3.0/J-SH07
		SoftBank 6-5 Series（Jフォン時代のもの）
		J-PHONE/4.0/J-SH51/SN******** SH/0001a Profile/MIDP-1.0 Configuration/CLDC-1.0
		SoftBank 3G Series（ボーダフォン時代のもの）
		Vodafone/1.0/V904SH/SHJ001/SN■Browser/VF-NetFront/3.3■Profile/MIDP-2.0■Configuration/CLDC-1.1
		SoftBank 3G Series（ソフトバンク時代のもの）
		SoftBank/1.0/910T/TJ001/SN■Browser/NetFront/3.3■Profile/MIDP-2.0■Configuration/CLDC-1.1
		基本的に、J-PHONE（or Vodafone or SoftBank）/ブラウザバージョン/機種名/その他、になってます。
		*/
		$browser = array ( //reversed array
		"DoCoMo",
		"UP.Browser",
		"SoftBank",
		"Vodafone",
		"J-PHONE",
		);

		foreach ($browser as $parent)
		{
			if ( ($s = strpos(strtoupper($_SERVER['HTTP_USER_AGENT']), strtoupper($parent))) !== FALSE )
			{
				$info['browser'] = $parent;
				switch ($parent){
					case "DoCoMo":
						if(strpos($_SERVER['HTTP_USER_AGENT'], "DoCoMo/1.0") >= 0
						and strpos($_SERVER['HTTP_USER_AGENT'], "/", 11) >= 0){
							$device = substr($_SERVER['HTTP_USER_AGENT'], 11, (strpos($_SERVER['HTTP_USER_AGENT'], "/", 11) - 11));
						}elseif(strpos($_SERVER['HTTP_USER_AGENT'], "DoCoMo/2.0") >= 0
						and strpos($_SERVER['HTTP_USER_AGENT'], "(", 11) >= 0){
							$device = substr($_SERVER['HTTP_USER_AGENT'], 11, (strpos($_SERVER['HTTP_USER_AGENT'], "(", 11) - 11));
						}else{
							$device = substr($_SERVER['HTTP_USER_AGENT'], 11);
						}
						break;
					case "UP.Browser":
						$device = substr($_SERVER['HTTP_USER_AGENT'], (strpos($_SERVER['HTTP_USER_AGENT'], "-") + 1), (strpos($_SERVER['HTTP_USER_AGENT'], " ") - strpos($_SERVER['HTTP_USER_AGENT'], "-") - 1));
						$info['browser'] = 'AU';
						break;
					default:
						if(isset($_SERVER['HTTP_X_JPHONE_MSNAME'])){
							$device = $_SERVER['HTTP_X_JPHONE_MSNAME'];
						}
						break;
				}
				if(!empty($device)){
					$info['version'] = $device;
					$info['type'] = 'mobile';
				}
				break; // first match wins
			}
		}
		if(!isset($info['browser']) or empty($info['browser'])){
			$info['browser'] = "OTHER";
		}
		if(!isset($info['version']) or empty($info['version'])){
			$info['version'] = $_SERVER['HTTP_USER_AGENT'];
		}
		if(!isset($info['type']) or empty($info['type'])){
			$info['type'] = 'none';
		}
		return $info;
	}
}